package cn.weibo.springmvc.handler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

public class ApplicationExceptionHandler implements HandlerExceptionResolver {

	private final Logger log = LoggerFactory.getLogger(ApplicationExceptionHandler.class);
	
	@Override
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		log.error("application exception", ex);
		log.error("hander = {}, exception message = {}, exception name = {}", new Object[] { handler, ex.getMessage(), ex.getClass().getName() });
		ModelAndView mdv = new ModelAndView("/views/error/message");
		mdv.addObject("message", "系统错误");
		DateTime dateTime = new DateTime();
		mdv.addObject("time", dateTime.toString("HH:mm:ss"));
		mdv.addObject("date", dateTime.toString("yyyy-MM-dd"));
		response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		
		return mdv;
	}

}
